package com.example.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.graduation.dto.EmployeeDO;
import com.graduation.mapper.EmployeeMapper;
import com.graduation.mapper.OrganizationMapper;
import com.graduation.pojo.*;
import com.graduation.pojo.EmployeeQuery;
import com.graduation.pojo.EmployeeRequest;
import com.graduation.pojo.OrganizationQuery;
import com.graduation.vo.EmployeeVO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Controller
@WebException
@RequestMapping("/employee")
public class EmployeeController extends BaseController<EmployeeDO, EmployeeVO> {

    @Resource
    private EmployeeMapper employeeMapper;
    @Resource
    private OrganizationMapper organizationMapper;

    @ResponseBody
    @GetMapping("/get")
        public WebResult<EmployeeVO> get(Integer id) {
        return new WebResult<>(getVoByDo(employeeMapper.selectById(id), EmployeeVO.class));
    }

    @ResponseBody
    @PostMapping("/page")
    public WebResult<PageResult<EmployeeVO>> page(EmployeeQuery query) {
        PageResult<EmployeeVO> result = new PageResult<>();
        result.setRowCount(employeeMapper.selectCount(query));
        result.setPageCount((result.getRowCount() - 1) / query.getPageSize() + 1);
        if (query.getPageNum() > result.getPageCount()) {
            query.setPageNum(result.getPageCount());
        }
        result.setPageNum(query.getPageNum());
        // 设置分页查询偏移量
        query.setLimit((query.getPageNum() - 1) * query.getPageSize());
        result.setEntityList(employeeMapper.selectList(query).stream()
                .map(e -> {
                    EmployeeVO employee = getVoByDo(e, EmployeeVO.class);
                    if (employee.getRole().equals("admin")) {
                        employee.setRole("平台管理员");
                    } else if (employee.getRole().equals("tester")) {
                        employee.setRole("渗透测试员");
                    } else {
                        employee.setRole("资产管理员");
                    }
                    return employee;
                })
                .collect(Collectors.toList()));
        return new WebResult<>(result);
    }

    @ResponseBody
    @PostMapping("/operate")
    public WebResult<?> operate(HttpServletRequest servletRequest, EmployeeRequest request) {
        EmployeeDO employeeDO = getDoByRequest(request, EmployeeDO.class);
        if (Objects.isNull(employeeDO.getId())) {
            employeeDO.setCreateTime(LocalDateTime.now());
            employeeDO.setUpdateTime(LocalDateTime.now());
            employeeMapper.insert(employeeDO);
        } else {
            employeeDO.setUpdateTime(LocalDateTime.now());
            employeeMapper.updateById(employeeDO);
        }
        return WebResult.success();
    }

    @ResponseBody
    @DeleteMapping("/delete")
    public WebResult<?> delete(Integer id) {
        employeeMapper.deleteById(id);
        return WebResult.success();
    }

    @RequestMapping("/to-employee")
    public String toEmployee() {
        return "employee/employeeAdmin";
    }

    @RequestMapping("/to-operate")
    public String toOperate(Model model, Integer id) {
        model.addAttribute("id", id);
        model.addAttribute("organizationList", organizationMapper.selectList(new OrganizationQuery()));
        model.addAttribute("employee", Objects.isNull(id) ? new EmployeeDO() : employeeMapper.selectById(id));
        return "employee/operateEmployee";
    }

    @PostMapping("/login")
    public String login(EmployeeRequest loginer, HttpServletRequest request) {
        EmployeeQuery query = new EmployeeQuery();
        query.setAccount(loginer.getAccount());
        query.setPassword(loginer.getPassword());
        List<EmployeeDO> userList = employeeMapper.selectList(query);
        if (CollectionUtil.isEmpty(userList)) {
            request.setAttribute("loginInfo", "用户名或密码错误");
            return "index";
        }
        request.getSession().setAttribute("loginer", getVoByDo(userList.get(0), EmployeeVO.class));
        request.setAttribute("loginerAccount", loginer.getAccount());
        return "redirect:/main";
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request, String loginInfo) {
        request.getSession().removeAttribute("loginer");
        return "index";
    }
}
